package com.aida.evil.eda.SQL;


import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

import com.aida.evil.eda.SQL.Eda_SQL.Item;
import com.aida.evil.eda.SQL.UserData.User_data;
import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.dao.RuntimeExceptionDao;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;

import java.sql.SQLException;

/****************************************************
 * com.aida.evil.eda.SQL.d
 * 作者：白雨濃  evil
 * 时间：16-3-23 07:20
 * 邮箱：evilbai@foxmail.com
 * 页面用途：
 ****************************************************/


public class Eda_DataBaseHelper extends OrmLiteSqliteOpenHelper
{

    /*数据库名称*/
    private static final String DATABASE_NAME = "Eda.db";
    /*数据库版本Version*/
    private static final int DATABASE_VERSION = 1;

    /*包含两个泛型,第一个泛型表DAO操作的类,第二个表示操作类的主键类型*/
    private Dao<Item, Integer> itemDao;
    /*主要是处理了过多繁琐的try…catch…的书写，和一些语法错误带来的崩溃，建议使用。*/
    private RuntimeExceptionDao<Item, Integer> runtimeExceptionDao;


    private Dao<User_data, Integer> userDataDao;
    private RuntimeExceptionDao<User_data, Integer> userDataRuntimeExceptionDao;


    public Eda_DataBaseHelper(Context context)
    {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    public Eda_DataBaseHelper(Context context, String databaseName, SQLiteDatabase.CursorFactory factory, int databaseVersion)
    {
        super(context, databaseName, factory, databaseVersion);
    }

    /**
     * 新建表
     *
     * @param sqLiteDatabase
     * @param connectionSource
     */
    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase, ConnectionSource connectionSource)
    {
        try
        {
                /*建立Item表*/
            TableUtils.createTable(connectionSource, Item.class);
                /*初始化Dao*/
            itemDao = getItemDao();
                /*初始化RuntimeExceptionDao*/
            runtimeExceptionDao = getItemRuntimeExceptionDao();


                  /*建立User_data表*/
            TableUtils.createTable(connectionSource, User_data.class);
                /*初始化Dao*/
            userDataDao = getUserDataDao();
                /*初始化RuntimeExceptionDao*/
            userDataRuntimeExceptionDao = getUserRuntimeExceptionDao();
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }
    }

    /**
     * 更新表
     *
     * @param sqLiteDatabase
     * @param connectionSource
     * @param i
     * @param i1
     */
    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, ConnectionSource connectionSource, int i, int i1)
    {
        try
        {
            /*升级表*/
            TableUtils.dropTable(connectionSource, Item.class, true);
            /*升级表*/
            TableUtils.dropTable(connectionSource, User_data.class, true);
            /*升级后建立新表*/
            onCreate(sqLiteDatabase, connectionSource);
        }
        catch (SQLException e)
        {
            e.printStackTrace();
        }
    }

    /**
     * 如果itemDao为null，则获得itemDao，否则直接赋值
     *
     * @return
     *
     * @throws SQLException
     */
    private Dao<Item, Integer> getItemDao() throws SQLException
    {
        if (itemDao == null)
        {
            itemDao = getDao(Item.class);
        }
        return itemDao;
    }

    /**
     * 如果runtimeExceptionDao为null，则获得runtimeExceptionDao，否则直接赋值
     *
     * @return
     */
    public RuntimeExceptionDao<Item, Integer> getItemRuntimeExceptionDao()
    {
        if (runtimeExceptionDao == null)
        {
            runtimeExceptionDao = getRuntimeExceptionDao(Item.class);
        }
        return runtimeExceptionDao;
    }

    /**
     * 释放链接
     */
    @Override
    public void close()
    {
        super.close();
        runtimeExceptionDao = null;
        userDataRuntimeExceptionDao=null;
    }

    public Dao<User_data, Integer> getUserDataDao() throws SQLException
    {
        if (userDataDao == null)
        {
            userDataDao = getDao(User_data.class);
        }
        return userDataDao;
    }

    public RuntimeExceptionDao<User_data, Integer> getUserRuntimeExceptionDao()
    {
        if (userDataRuntimeExceptionDao == null)
        {
            userDataRuntimeExceptionDao = getRuntimeExceptionDao(User_data.class);
        }
        return userDataRuntimeExceptionDao;
    }
}
